package com.zhongyu.controller;

import com.zhongyu.pojo.Order;
import com.zhongyu.pojo.OrderDetail;
import com.zhongyu.pojo.User;
import com.zhongyu.service.CaiGouService;
import com.zhongyu.service.OrderDetailService;
import com.zhongyu.service.OrderService;
import com.zhongyu.vo.OrderVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/caiGou/sq/")
public class CaiGouController {
    @Resource
    private OrderDetailService orderDetailService;
    @Resource
    private CaiGouService caiGouService;
    @Resource
    private OrderService orderService;
    @GetMapping("/search/{pageNo}/{pageSize}")
    @RequiresPermissions("shenqing:list")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize){
        PageResult pageResult=caiGouService.searchAll(pageNo, pageSize);
        System.out.println(pageResult);
        return ResultVo.success(pageResult);
    }
    @GetMapping("/search")
    public ResultVo search(Integer id){
        System.out.println(id);
        List<Order> pageResult=caiGouService.searchAll(id);
        return ResultVo.success(pageResult);
    }
    @GetMapping("/list")
    public ResultVo list(Integer id){
       List<OrderDetail> list=orderDetailService.searchAll(id);
        return ResultVo.success(list);
    }
    @GetMapping("/salList/{pageNo}/{pageSize}")
    public ResultVo searchSal(@PathVariable Integer pageNo, @PathVariable Integer pageSize, OrderVo orderVo){
        PageResult pageResult= orderService.searchSal(pageNo, pageSize, orderVo);
        return ResultVo.success(pageResult);
    }
    @GetMapping("/updateState")//post请求要加@RequestBody进行接收
    public ResultVo update(Order order) {
        try {
            LocalDateTime localDateTime=order.setPurchaseTime(LocalDateTime.now());
            String state= order.setState(Order.STATE_START);
            Integer id=order.getId();
            orderService.update(state,localDateTime,id);
            return ResultVo.success("确认订单成功");
        } catch (Exception ex) {
            return ResultVo.failure("确认订单失败", ex);
        }

    }
    @GetMapping("/list1")
    public ResultVo list(){
        List<Order> list=orderService.searchAll();
        return ResultVo.success(list);
    }
    @GetMapping("/list/{pageNo}/{pageSize}")
    public ResultVo list(@PathVariable Integer pageNo, @PathVariable Integer pageSize, OrderVo orderVo){
        PageResult pageResult= orderService.searchAll(pageNo, pageSize, orderVo);
        return ResultVo.success(pageResult);
    }
/*    @PostMapping("/add")
    public ResultVo add(@RequestBody Order order, HttpSession session){
        try {
            System.out.println("1111"+order);
            long now = System.currentTimeMillis();
            //获取4位年份数字
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy");
            //获取时间戳
            String time=dateFormat.format(now);
            String info=now+"";
            //获取三位随机数
            int ran=(int) ((Math.random()*9+1)*100);
            order.setCreateTime(LocalDateTime.now());
            order.setPurchaseTime(LocalDateTime.now());
            order.setWayBillCode(time+info.substring(2,info.length())+ran);
            User loginUser= (User) session.getAttribute("loginUser");
            if (loginUser!=null){
                order.setCreator(loginUser.getId());
                order.setPurchaser(loginUser.getId());
            }
            order.setType(1);
            order.setState(Order.STATE_CREATE);
            caiGouService.add(order);
            return ResultVo.success("采购成功！");
        }catch (Exception ex){
            return ResultVo.failure("采购失败！",ex);
        }
    }*/


    @PostMapping("/add1")
    public ResultVo add(@RequestBody OrderDetail orderDetail,HttpSession session){
        try {

            orderDetailService.add(orderDetail,session);
            return ResultVo.success("采购申请成功！");
        }catch (Exception ex){
            return ResultVo.failure("采购申请失败！",ex);
        }
    }

    @PostMapping("/add2")
    public ResultVo add2(@RequestBody OrderDetail orderDetail,HttpSession session){
        try {
            orderDetailService.add2(orderDetail,session);
            return ResultVo.success("销售录入成功！");
        }catch (Exception ex){
            return ResultVo.failure("销售录入失败！",ex);
        }
    }
}
